/*
	This file is part of cpp-ethereum.

	cpp-ethereum is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	cpp-ethereum is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with cpp-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file MixClient.h
 * @author Yann yann@ethdev.com
 * @author Arkadiy Paronyan arkadiy@ethdev.com
 * @date 2015
 * Ethereum IDE client.
 */

#pragma once

#include <vector>
#include <string>
#include <libethcore/BasicAuthority.h>
#include <libethereum/ExtVM.h>
#include <libethereum/ClientBase.h>
#include <libethereum/Client.h>
#include "MachineStates.h"

namespace dev
{
namespace eth { class EnvInfo; }

namespace mix
{

class NoProof
{
	class BlockHeaderRaw: public dev::eth::BlockInfo
	{
	public:
		static const unsigned SealFields = 0;

	protected:
		BlockHeaderRaw() = default;
		BlockHeaderRaw(BlockInfo const& _bi): BlockInfo(_bi) {}

		void populateFromHeader(RLP const& _header, dev::eth::Strictness _s) { (void) _header; (void) _s; }
		void populateFromParent(BlockHeaderRaw const& _parent) { (void)_parent; }
		void streamRLPFields(RLPStream& _s) const { (void) _s; }
	};

public:

	static std::string name() { return "NoProof"; }
	static unsigned revision() { return 0; }
	using BlockHeader = dev::eth::BlockHeaderPolished<BlockHeaderRaw>;

private:
	static AddressHash s_authorities;
};

class MixBlockChain: public dev::eth::FullBlockChain<NoProof>
{
public:
	MixBlockChain(std::string const& _path, h256 _stateRoot);

	static bytes createGenesisBlock(h256 _stateRoot);
};

class MixClient: public dev::eth::ClientBase
{
public:
	MixClient(std::string const& _dbPath);
	virtual ~MixClient();
	/// Reset state to the empty state with given balance.
	void resetState(std::unordered_map<dev::Address, dev::eth::Account> const& _accounts,  Secret const& _miner = Secret());
	void mine();
	ExecutionResult lastExecution() const;
	ExecutionResult execution(unsigned _index) const;

	dev::eth::ExecutionResult call(Address const& _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, eth::BlockNumber _blockNumber = eth::PendingBlock, eth::FudgeFactor _ff = eth::FudgeFactor::Strict) override;
	dev::eth::ExecutionResult create(Address const& _secret, u256 _value, bytes const& _data = bytes(), u256 _gas = 10000, u256 _gasPrice = 10 * eth::szabo, eth::BlockNumber _blockNumber = eth::PendingBlock, eth::FudgeFactor _ff = eth::FudgeFactor::Strict) override;

	using ClientBase::submitTransaction;
	virtual std::pair<h256, Address> submitTransaction(eth::TransactionSkeleton const& _ts, Secret const& _secret) override { return submitTransaction(_ts, _secret, false); }
	std::pair<h256, Address> submitTransaction(eth::TransactionSkeleton const& _ts, Secret const& _secret, bool _gasAuto);
	dev::eth::ExecutionResult call(Address const& _secret, u256 _value, Address _dest, bytes const& _data, u256 _gas, u256 _gasPrice, eth::BlockNumber _blockNumber, bool _gasAuto, eth::FudgeFactor _ff = eth::FudgeFactor::Strict);
	ExecutionResult debugTransaction(dev::eth::Transaction const& _t, eth:: State const& _state, eth::EnvInfo const& _envInfo, bool _call);
	void setBeneficiary(Address _us) override;
	void startMining() override;
	void stopMining() override;
	bool isMining() const override;
	u256 hashrate() const override;
	eth::WorkingProgress miningProgress() const override;
	virtual void flushTransactions() override {}

	/// @returns the last mined block information
	using Interface::blockInfo; // to remove warning about hiding virtual function
	eth::BlockInfo blockInfo() const;

	/// return the new address generated by the last tr (if creation). returns empty address if other cases.
	Address lastCreatedContractAddr() const;

protected:
	/// ClientBase methods
	using ClientBase::asOf;
	virtual dev::eth::Block asOf(h256 const& _block) const override;
	virtual dev::eth::BlockChain& bc() override { return *m_bc; }
	virtual dev::eth::BlockChain const& bc() const override { return *m_bc; }
	virtual dev::eth::Block preMine() const override { ReadGuard l(x_state);  return m_preMine; }
	virtual dev::eth::Block postMine() const override { ReadGuard l(x_state); return m_postMine; }
	virtual void prepareForTransaction() override {}

private:
	void executeTransaction(dev::eth::Transaction const& _t, eth::Block& _block, bool _call, bool _gasAuto, dev::Secret const& _secret = dev::Secret());
	dev::eth::Transaction replaceGas(dev::eth::Transaction const& _t, dev::u256 const& _gas, dev::Secret const& _secret = dev::Secret());

	eth::Block m_preMine;
	eth::Block m_postMine;
	OverlayDB m_stateDB;
	std::unique_ptr<MixBlockChain> m_bc;
	mutable boost::shared_mutex x_state;
	mutable boost::shared_mutex x_executions;
	ExecutionResults m_executions;
	std::string m_dbPath;
};

}

}
